home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap13 / Wanderer / DriveView.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  7.5 KB  |  276 lines

  1. //***********************************************************************
  2. //
  3. //  DriveView.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include <afxcview.h>
  9. #include "Resource.h"
  10. #include "DriveView.h"
  11.  
  12. // Image indexes
  13. #define ILI_HARD_DISK       0
  14. #define ILI_FLOPPY          1
  15. #define ILI_CD_ROM          2
  16. #define ILI_NET_DRIVE       0
  17. #define ILI_RAM_DRIVE       0
  18. #define ILI_CLOSED_FOLDER   3
  19. #define ILI_OPEN_FOLDER     4
  20.  
  21. IMPLEMENT_DYNCREATE (CDriveView, CTreeView)
  22.  
  23. BEGIN_MESSAGE_MAP (CDriveView, CTreeView)
  24.     ON_WM_CREATE ()
  25.     ON_NOTIFY_REFLECT (TVN_ITEMEXPANDING, OnItemExpanding)
  26.     ON_NOTIFY_REFLECT (TVN_SELCHANGED, OnSelChanged)
  27. END_MESSAGE_MAP ()
  28.  
  29. BOOL CDriveView::PreCreateWindow (CREATESTRUCT& cs)
  30. {
  31.     if (!CTreeView::PreCreateWindow (cs))
  32.         return FALSE;
  33.  
  34.     cs.style |= TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS |
  35.         TVS_SHOWSELALWAYS;
  36.     return TRUE;
  37. }
  38.  
  39. int CDriveView::OnCreate (LPCREATESTRUCT lpcs)
  40. {
  41.     if (CTreeView::OnCreate (lpcs) == -1)
  42.         return -1;
  43.  
  44.     m_imglDrives.Create (IDR_DRIVEIMAGES, 16, 1, RGB (255, 0, 255));
  45.     GetTreeCtrl ().SetImageList (&m_imglDrives, TVSIL_NORMAL);
  46.  
  47.     InitTree ();
  48.     return 0;
  49. }
  50.  
  51. void CDriveView::OnItemExpanding (NMHDR* pnmh, LRESULT* pResult)
  52. {
  53.     NM_TREEVIEW* pnmtv = (NM_TREEVIEW*) pnmh;
  54.     HTREEITEM hItem = pnmtv->itemNew.hItem;
  55.     CString string = GetPathFromNode (hItem);
  56.  
  57.     *pResult = FALSE;
  58.  
  59.     if (pnmtv->action == TVE_EXPAND) {
  60.         DeleteFirstChild (hItem);
  61.         if (AddDirectories (hItem, string) == 0)
  62.             *pResult = TRUE;
  63.     }
  64.     else { // pnmtv->action == TVE_COLLAPSE
  65.         DeleteAllChildren (hItem);
  66.         if (GetTreeCtrl ().GetParentItem (hItem) == NULL)
  67.             GetTreeCtrl ().InsertItem ("", ILI_CLOSED_FOLDER,
  68.                 ILI_CLOSED_FOLDER, hItem);
  69.         else
  70.             SetButtonState (hItem, string);
  71.     }
  72. }
  73.  
  74. void CDriveView::OnSelChanged (NMHDR* pnmh, LRESULT* pResult)
  75. {
  76.     NM_TREEVIEW* pnmtv = (NM_TREEVIEW*) pnmh;
  77.     CString strPath = GetPathFromNode (pnmtv->itemNew.hItem);
  78.     OnSelectionChanged (strPath);
  79. }
  80.  
  81. void CDriveView::OnSelectionChanged (CString& strPath)
  82. {
  83.     //
  84.     // Override this function in a derived class to respond to
  85.     // selection changes differently. Here, UpdateAllViews is used
  86.     // as a conduit for updating the companion CFileView.
  87.     //
  88.     GetDocument ()->UpdateAllViews (this, (LPARAM) (LPCTSTR) strPath);
  89. }
  90.  
  91. int CDriveView::InitTree ()
  92. {
  93.     int nPos = 0;
  94.     int nDrivesAdded = 0;
  95.     CString strDrive = "?:\\";
  96.  
  97.     DWORD dwDriveList = ::GetLogicalDrives ();
  98.  
  99.     while (dwDriveList) {
  100.         if (dwDriveList & 1) {
  101.             strDrive.SetAt (0, 0x41 + nPos);
  102.             if (AddDriveNode (strDrive))
  103.                 nDrivesAdded++;
  104.         }
  105.         dwDriveList >>= 1;
  106.         nPos++;
  107.     }
  108.     return nDrivesAdded;
  109. }
  110.  
  111. BOOL CDriveView::AddDriveNode (CString& strDrive)
  112. {
  113.     CString string;
  114.     HTREEITEM hItem;
  115.     static BOOL bFirst = TRUE;
  116.  
  117.     UINT nType = ::GetDriveType ((LPCTSTR) strDrive);
  118.  
  119.     switch (nType) {
  120.  
  121.     case DRIVE_REMOVABLE:
  122.         hItem = GetTreeCtrl ().InsertItem (strDrive, ILI_FLOPPY,
  123.             ILI_FLOPPY);
  124.         GetTreeCtrl ().InsertItem ("", ILI_CLOSED_FOLDER,
  125.             ILI_CLOSED_FOLDER, hItem);
  126.         break;
  127.  
  128.     case DRIVE_FIXED:
  129.         hItem = GetTreeCtrl ().InsertItem (strDrive, ILI_HARD_DISK,
  130.             ILI_HARD_DISK);
  131.         SetButtonState (hItem, strDrive);
  132.  
  133.         // If this is the first fixed disk, select and expand it
  134.         if (bFirst) {
  135.             GetTreeCtrl ().SelectItem (hItem);
  136.             GetTreeCtrl ().Expand (hItem, TVE_EXPAND);
  137.             bFirst = FALSE;
  138.         }
  139.         break;
  140.  
  141.     case DRIVE_REMOTE:
  142.         hItem = GetTreeCtrl ().InsertItem (strDrive, ILI_NET_DRIVE,
  143.             ILI_NET_DRIVE);
  144.         SetButtonState (hItem, strDrive);
  145.         break;
  146.  
  147.     case DRIVE_CDROM:
  148.         hItem = GetTreeCtrl ().InsertItem (strDrive, ILI_CD_ROM,
  149.             ILI_CD_ROM);
  150.         GetTreeCtrl ().InsertItem ("", ILI_CLOSED_FOLDER,
  151.             ILI_CLOSED_FOLDER, hItem);
  152.         break;
  153.  
  154.     case DRIVE_RAMDISK:
  155.         hItem = GetTreeCtrl ().InsertItem (strDrive, ILI_RAM_DRIVE,
  156.             ILI_RAM_DRIVE);
  157.         SetButtonState (hItem, strDrive);
  158.         break;
  159.  
  160.     default:
  161.         return FALSE;
  162.     }
  163.  
  164.     return TRUE;
  165. }
  166.  
  167. BOOL CDriveView::SetButtonState (HTREEITEM hItem, CString& strPath)
  168. {
  169.     HANDLE hFind;
  170.     WIN32_FIND_DATA fd;
  171.     BOOL bResult = FALSE;
  172.  
  173.     CString string = strPath;
  174.     if (string.Right (1) != "\\")
  175.         string += "\\";
  176.     string += "*.*";
  177.  
  178.     if ((hFind = ::FindFirstFile ((LPCTSTR) string, &fd)) ==
  179.         INVALID_HANDLE_VALUE)
  180.         return bResult;
  181.  
  182.     do {
  183.         if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  184.             CString strCmp = (LPCTSTR) &fd.cFileName;
  185.             if ((strCmp != ".") && (strCmp != "..")) {
  186.                 GetTreeCtrl ().InsertItem ("", ILI_CLOSED_FOLDER,
  187.                     ILI_CLOSED_FOLDER, hItem);
  188.                 bResult = TRUE;
  189.                 break;
  190.             }
  191.         }
  192.     } while (::FindNextFile (hFind, &fd));
  193.  
  194.     ::CloseHandle (hFind);
  195.     return bResult;
  196. }
  197.  
  198. CString CDriveView::GetPathFromNode (HTREEITEM hItem)
  199. {
  200.     CString strResult = GetTreeCtrl ().GetItemText (hItem);
  201.  
  202.     HTREEITEM hParent;
  203.     while ((hParent = GetTreeCtrl ().GetParentItem (hItem)) != NULL) {
  204.         CString string = GetTreeCtrl ().GetItemText (hParent);
  205.         if (string.Right (1) != "\\")
  206.             string += "\\";
  207.         strResult = string + strResult;
  208.         hItem = hParent;
  209.     }
  210.     return strResult;
  211. }
  212.  
  213. int CDriveView::AddDirectories (HTREEITEM hItem, CString& strPath)
  214. {
  215.     HANDLE hFind;
  216.     WIN32_FIND_DATA fd;
  217.     HTREEITEM hNewItem;
  218.  
  219.     int nCount = 0;
  220.  
  221.     CString string = strPath;
  222.     if (string.Right (1) != "\\")
  223.         string += "\\";
  224.     string += "*.*";
  225.  
  226.     if ((hFind = ::FindFirstFile ((LPCTSTR) string, &fd)) ==
  227.         INVALID_HANDLE_VALUE) {
  228.         if (GetTreeCtrl ().GetParentItem (hItem) == NULL)
  229.             GetTreeCtrl ().InsertItem ("", ILI_CLOSED_FOLDER,
  230.                 ILI_CLOSED_FOLDER, hItem);
  231.         return 0;
  232.     }
  233.  
  234.     do {
  235.         if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  236.             CString strCmp = (LPCTSTR) &fd.cFileName;
  237.             if ((strCmp != ".") && (strCmp != "..")) {
  238.                 hNewItem =
  239.                     GetTreeCtrl ().InsertItem ((LPCTSTR) &fd.cFileName,
  240.                     ILI_CLOSED_FOLDER, ILI_OPEN_FOLDER, hItem);
  241.  
  242.                 CString strNewPath = strPath;
  243.                 if (strNewPath.Right (1) != "\\")
  244.                     strNewPath += "\\";
  245.  
  246.                 strNewPath += (LPCTSTR) &fd.cFileName;
  247.                 SetButtonState (hNewItem, strNewPath);
  248.                 nCount++;
  249.             }
  250.         }
  251.     } while (::FindNextFile (hFind, &fd));
  252.  
  253.     ::CloseHandle (hFind);
  254.     return nCount;
  255. }
  256.  
  257. void CDriveView::DeleteFirstChild (HTREEITEM hParent)
  258. {
  259.     HTREEITEM hItem;
  260.     if ((hItem = GetTreeCtrl ().GetChildItem (hParent)) != NULL)
  261.         GetTreeCtrl ().DeleteItem (hItem);
  262. }
  263.  
  264. void CDriveView::DeleteAllChildren (HTREEITEM hParent)
  265. {
  266.     HTREEITEM hItem;
  267.     if ((hItem = GetTreeCtrl ().GetChildItem (hParent)) == NULL)
  268.         return;
  269.  
  270.     do {
  271.         HTREEITEM hNextItem = GetTreeCtrl ().GetNextSiblingItem (hItem);
  272.         GetTreeCtrl ().DeleteItem (hItem);
  273.         hItem = hNextItem;
  274.     } while (hItem != NULL);
  275. }
  276.